package com.canaan.business.controller.pc.storehouse;

import java.util.List;

import com.canaan.business.domain.storehouse.StockFreeze;
import com.canaan.business.service.storehouse.IStockFreezeService;
import com.canaan.common.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.compress.utils.Lists;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.enums.BusinessType;
import javax.annotation.Resource;
import com.canaan.common.utils.poi.ExcelUtil;
import com.canaan.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import static org.springframework.util.CollectionUtils.*;

/**
 * 库存冻结Controller
 *
 * @author ruoyi
 * @date 2023-02-08
 */
@Api(tags = "库存冻结")
@RestController
@RequestMapping("/storehouse/freeze")
public class StockFreezeController extends BaseController {
    @Resource
    private IStockFreezeService stockFreezeService;

    /**
     * 查询库存冻结列表
     */
    @ApiOperation(value = "查询库存冻结列表")
    @GetMapping("/list")
    public TableDataInfo list(StockFreeze stockFreeze) {
        startPage();
        List<StockFreeze> list = stockFreezeService.selectStockFreezeList(stockFreeze);
        return getDataTable(list);
    }

    /**
     * 导出库存冻结列表
     */
    @ApiOperation(value = "导出库存冻结列表")
    @Log(title = "库存冻结", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockFreeze stockFreeze) {
        List<StockFreeze> list = stockFreezeService.selectStockFreezeList(stockFreeze);
        ExcelUtil<StockFreeze> util = new ExcelUtil<StockFreeze>(StockFreeze.class);
        util.exportExcel(response, list, "库存冻结数据");
    }

    /**
     * 获取库存冻结详细信息
     */
    @ApiOperation(value = "获取库存冻结详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(stockFreezeService.selectStockFreezeById(id));
    }

    /**
     * 新增库存冻结
     */
    @ApiOperation(value = "新增库存冻结")
    @Log(title = "库存冻结", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockFreeze stockFreeze) {
        stockFreezeService.insertStockFreeze(stockFreeze);
        return toAjax(1);
    }

    /**
     * 激活库存冻结
     */
    @ApiOperation(value = "激活库存冻结")
    @Log(title = "库存冻结", businessType = BusinessType.INSERT)
    @PostMapping("/activation/{id}")
    public AjaxResult activation(@PathVariable("id") Long id) {
        return toAjax(stockFreezeService.activationStockFreezeAndOccupy(id));
    }

    /**
     * 取消库存冻结
     */
    @ApiOperation(value = "取消库存冻结")
    @Log(title = "库存冻结", businessType = BusinessType.INSERT)
    @PostMapping("/removeActivation/{id}")
    public AjaxResult removeActivation(@PathVariable("id") Long id) {
        return toAjax(stockFreezeService.removeActivationStockFreezeAndOccupy(id));
    }

    /**
     * 修改库存冻结
     */
    @ApiOperation(value = "修改库存冻结")
    @Log(title = "库存冻结", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockFreeze stockFreeze) {
        return toAjax(stockFreezeService.updateStockFreeze(stockFreeze));
    }

    /**
     * 删除库存冻结
     */
    @ApiOperation(value = "删除库存冻结")
    @Log(title = "库存冻结", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(stockFreezeService.deleteStockFreezeByIds(ids));
    }

    /**
     * 库区导入模板
     */
    @ApiOperation(value = "库存冻结导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<StockFreeze> util = new ExcelUtil<StockFreeze>(StockFreeze.class);
        util.importTemplateExcel(response, "库存冻结数据");
    }

    /**
     * 导入库位冻结列表
     */
    @PostMapping("/importExcel")
    @ApiOperation(value = "导入库存冻结列表")
    @Log(title = "导入库存冻结列表", businessType = BusinessType.IMPORT)
    public AjaxResult importExcel(MultipartFile file)
    {
        List<StockFreeze> list;
        try {
            //读取excel文件数据
            ExcelUtil<StockFreeze> util = new ExcelUtil<StockFreeze>(StockFreeze.class);
            list = util.importExcel(file.getInputStream());
        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("导入文件解析失败");
        }
        //初步校验数据
        if(isEmpty(list)) {
            throw new ServiceException("文件缺少数据");
        }
        //深入校验数据和保存数据
        List<String> errorList = stockFreezeService.importExcel(list);
        if(!errorList.isEmpty()){
            StringBuilder failureMsg = new StringBuilder();
            failureMsg.append("很抱歉，导入失败！共 ").append(errorList.size()).append(" 条数据格式不正确，错误如下：");
            for (int i = 0; i < errorList.size(); i++) {
                failureMsg.append("<br/>").append(i + 1).append("、").append(errorList.get(i));
            }
            return error(failureMsg.toString());
        }
        return success("导入成功");
    }
}
